package cn.yzgy.yzgyoa.ucenter.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.constant.YzCacheName;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.shiro.AuthRealm;
import cn.yzgy.yzgyoa.ucenter.dto.MenuItemDto;
import cn.yzgy.yzgyoa.ucenter.service.impl.SysMenuServiceImpl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import cn.yzgy.yzgyoa.common.controller.BaseController;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 菜单表，用于储存系统中的菜单，权限等信息 前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-27
 */
@Slf4j
@Syslog
@RestController
@RequestMapping("/ucenter/menu")
@CacheConfig(cacheNames = YzCacheName.MENU_CACHE_NAME)
public class SysMenuController extends BaseController {

    private final SysMenuServiceImpl sysMenuService;
    private final AuthRealm authRealm;

    public SysMenuController(SysMenuServiceImpl sysMenuService, AuthRealm authRealm) {
        this.authRealm = authRealm;
        this.sysMenuService = sysMenuService;
    }

    @GetMapping
    @ApiDesc("获取用户菜单")
    @Cacheable(keyGenerator = YzCacheName.GENERATOR_NAME)
    public Object getMenu() {
        return Urmf.success(getCurrentMethodName(), sysMenuService.getUserTreeSysMenu(authRealm.getPermissions()));
    }

    /**
     * 按照最高权限获取菜单
     *
     * @return 所有菜单
     */
    @GetMapping("full")
    @ApiDesc(value = "获取完全用户菜单")
    //@RequiresPermissions("api:ucenter:menu:get_full")
    public Urmf getFullMenu() {
        Set<String> permissions = new HashSet<String>() {{
            add("*");
        }};
        return Urmf.success(getCurrentMethodName(), sysMenuService.getTreeSysMenuDto(permissions));
    }

    @GetMapping("list")
    @ApiDesc(value = "获取菜单项目列表")
    @RequiresPermissions("api:ucenter:menu:list")
    public Urmf getMenuList(Integer page, Integer size) {
        return Urmf.success(getCurrentMethodName(), sysMenuService.page(new Page<>(page, size)));
    }

    @DeleteMapping
    @ApiDesc(value = "删除菜单项", log = true)
    @RequiresPermissions("api:ucenter:menu:delete")
    public Urmf deleteMenuItem(@RequestBody String mIdStr) {
        int mId = JSON.parseObject(mIdStr).getInteger("mId");
        return Urmf.success(getCurrentMethodName(), sysMenuService.deleteMenuItem(mId));
    }

    @PutMapping
    @ApiDesc(value = "更新菜单项", log = true)
    @RequiresPermissions("api:ucenter:menu:update")
    public Urmf updateMenuItem(@RequestBody @Valid MenuItemDto menuItemDto) {
        return Urmf.success(getCurrentMethodName(), sysMenuService.updateMenu(menuItemDto));
    }

    @PostMapping
    @ApiDesc(value = "新增菜单项", log = true)
    @RequiresPermissions("api:ucenter:menu:add")
    public Urmf addMenuItem(@RequestBody @Valid MenuItemDto menuItemDto) {
        return Urmf.success(getCurrentMethodName(), sysMenuService.addNewMenu(menuItemDto));
    }
}
